﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Web;
using Monocle.DataProvider;

namespace Monocle.Controllers.StaticHelpers
{
    public class Mail
    {
        public static void SendMail(String recipient, String mailContent, String subject)
        {
            try
            {
                //Read settings regarding configuration of the SMTP server in Web.Config
                var appSettings = ConfigurationManager.AppSettings;
                var mailFromEmail = appSettings["SMTP_SenderEmail"];
                var mailFromName = appSettings["SMTP_SenderName"];
                var mailHost = appSettings["SMTP_Host"];
                var mailPort = appSettings["SMTP_Port"];
                var mailUsername = appSettings["SMTP_Username"];
                var mailPassword = appSettings["SMTP_Password"];

                //Construct the actual mail
                var fromMail = new MailAddress(mailFromEmail, mailFromName);
                var toMail = new MailAddress(recipient, recipient);

                using (var message = new MailMessage(fromMail, toMail))
                {
                    var smtpClient = new SmtpClient(mailHost, Convert.ToInt32(mailPort));
                    var credentials = new System.Net.NetworkCredential(mailUsername, mailPassword);
                    smtpClient.Credentials = credentials;
                    message.Subject = subject;
                    message.Body = mailContent;
                    smtpClient.Send(message);
                }
            }
            catch (Exception e)
            {

                throw e;
            }
        }
    }
}